const express=require('express');
const wearService=require('../services/wearService');
const carService=require('../services/carService');
var router=express.Router();

router.use('/things',async (req,res,next)=>{
    var data =await carService.findByAll();
    if(data.success){
        res.send(data.things);
    }else{
        res.send(data.msg);
    }
})
router.use('/kids',async (req,res,next)=>{
    var post=req.body;
    var imgsrc=post.imgsrc;
    var wear =await wearService.findByImgsrc(imgsrc);
    if(wear.success){
        var data =await carService.addThing(wear.wears[0]);
    }
})
router.use('/mens',async (req,res,next)=>{
    var post=req.body;
    var imgsrc=post.imgsrc;
    var wear =await wearService.findByImgsrc(imgsrc);
    if(wear.success){
        var data =await carService.addThing(wear.wears[0]);
    }
})
router.use('/women',async (req,res,next)=>{
    var post=req.body;
    var imgsrc=post.imgsrc;
    var wear =await wearService.findByImgsrc(imgsrc);
    if(wear.success){
        var data =await carService.addThing(wear.wears[0]);
    }
})

router.use('/delete',async (req,res,next)=>{
    var post=req.body;
    var imgsrc=post.imgsrc;
    // console.log('imgsrc',imgsrc);
    var data=await carService.deleteThing(imgsrc);
    if(data.success){
        res.send(data.things);
    }else{
        res.send(data.msg);
    }
})
router.use('/modify',async (req,res,next)=>{
    var post=req.body;
    var number=post.number;
    var imgsrc=post.imgsrc;
    console.log('number',number);
    console.log('imgsrc',imgsrc);
    var data=await carService.modifyThing(imgsrc,number);
    if(data.success){
        res.send(data.things);
    }else{
        res.send(data.msg);
    }
})
exports.router=router;